package com.nineclock.signIn.service.impl;
import com.nineclock.signIn.dto.SignInfoDepartmentDTO;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.filter.CurrentUserHolder;
import com.nineclock.common.utils.DateTimeUtil;
import com.nineclock.signIn.dto.SignInfoListDTO;
import com.nineclock.signIn.dto.SignInfoRecordDTO;
import com.nineclock.signIn.mapper.SignInfoMapper;
import com.nineclock.signIn.pojo.SignInfo;
import com.nineclock.signIn.service.SigInListService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.SysCompanyUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author : Liu
 * @date : 2021/12/2 0002 16:04
 */
@Service
@Transactional
public class SigInListServiceImpl implements SigInListService {
    @Autowired
    private SignInfoMapper signInfoMapper;

    @Autowired
    private SysCompanyUserFeign companyUserFeign;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageResult<SignInfoListDTO> signList(String endTime, String startTime, Integer page, Integer pageSize) {
        // 创建分页对象，指定分页参数，参数1-当前页，参数2-页大小
        IPage<SignInfo> ipage = new Page<>(page, pageSize);

        // 获取两个日期之间的日期集合
        List<String> spanDateList = DateTimeUtil.getSpanDateList(startTime, endTime);

        // 获取两个日期之间签到过的员工id集合
        LambdaQueryWrapper<SignInfo> wrapper_companyUserId = new LambdaQueryWrapper<>();
        wrapper_companyUserId.eq(SignInfo::getCompanyId, CurrentUserHolder.get().getCompanyId());
        wrapper_companyUserId.between(SignInfo::getCreateDate, startTime,endTime);

        // 获得员工Id
        List<SignInfo> signInfoList = signInfoMapper.selectList(wrapper_companyUserId);
        Set<Long> CompanyUserIdList = signInfoList.stream().map(signInfo ->signInfo.getCompanyUserId()).collect(Collectors.toSet());
        if (CompanyUserIdList == null) {
            return null;
        }

        List<SignInfoListDTO> signInfoListDTOList = new ArrayList<>();
        for (Long companyUserId : CompanyUserIdList) {
            //通过员工id获得员工数据
            SysCompanyUserDTO companyUserDTO = companyUserFeign.queryCompanyUserById(companyUserId).getData();

            //写入普通数据
            SignInfoListDTO signInfoListDTO = new SignInfoListDTO();
            signInfoListDTO.setId(companyUserId);
            signInfoListDTO.setPost(companyUserDTO.getPost());
            signInfoListDTO.setUsername(companyUserDTO.getUserName());
            signInfoListDTO.setWorkNumber(companyUserDTO.getWorkNumber());
            signInfoListDTO.setDepartment(new SignInfoDepartmentDTO(companyUserDTO.getDepartmentId(), companyUserDTO.getDepartmentName()));

            //写入日期签到数据
            List<SignInfoRecordDTO> signInfoRecordDTOList = new ArrayList<>();
            for (String Date : spanDateList) {

                //分割年月日
                String[] date = Date.split("-");
                String month = date[1].replaceFirst("^0*", "");
                String day = date[2].replaceFirst("^0*", "");

                String key = "SignInMonth" + CurrentUserHolder.get().getCompanyId() + CurrentUserHolder.get().getCompanyUserId() + month + day;
                int count = 0;
                if (Boolean.TRUE.equals(redisTemplate.hasKey(key))) {   //已存在就修改后存入
                    count = (int)redisTemplate.opsForValue().get(key);
                }
                SignInfoRecordDTO signInfoRecordDTO = new SignInfoRecordDTO();
                signInfoRecordDTO.setSignDate(Date);
                signInfoRecordDTO.setSignCount(Long.valueOf(count));
                signInfoRecordDTOList.add(signInfoRecordDTO);
            }
            signInfoListDTO.setSigns(signInfoRecordDTOList);

            signInfoListDTOList.add(signInfoListDTO);
        }
        return new PageResult<SignInfoListDTO>(1L, 1L, signInfoListDTOList);
    }
}
